#!/usr/bin/env python

import glob
import os
import itk
import numpy as np
def count(filename):
    reader = itk.ImageFileReader.ISS3.New()
    reader.SetFileName(filename)
    reader.Update()
    img = reader.GetOutput()
    array = itk.PyBuffer.ISS3.GetArrayFromImage(img)
    array = np.where(array>0) 
    count1 = len(array[0])*1.0
    return count1

def file_size(filename):
    reader = itk.ImageFileReader.ISS3.New()
    reader.SetFileName(filename)
    reader.Update()
    img = reader.GetOutput()
    array = itk.PyBuffer.ISS3.GetArrayFromImage(img)
    print array.shape
    count1 = array.shape[0]*array.shape[1]*array.shape[2]*1.0
    return count1

def main(): 
  os.chdir("/home/xsong/Code2")
  fo = open("convexHullAnalysis.txt","w")
  dirs = glob.glob("PE000??")
  print dirs
  for d in dirs:
     print d
     fname1 = os.path.join(d, d+".mha")
     fname2 = os.path.join(d, d+"_lung_hull.mha" )
     if os.path.exists(fname2) :
        b = file_size(fname1) 
        a = count(fname2)
        r = a*1.0/b
        print a, b, r 
        fo.write("%s:%s\n"%(d, r))
  fo.close()
if __name__ == '__main__':
    main()
